import 'package:flutter_test/flutter_test.dart';
import 'package:lmmi/common/services/apple_sign_in_service.dart';

void main() {
  group('Apple Sign In Service Tests', () {
    test('should create apple user with valid data', () {
      // Arrange
      const userIdentifier = 'test_user_123';
      const email = 'test@example.com';
      const displayName = 'Test User';

      // Act
      final user = AppleSignInService.createAppleUser(
        userIdentifier: userIdentifier,
        email: email,
        displayName: displayName,
      );

      // Assert
      expect(user.id, equals('apple_$userIdentifier'));
      expect(user.username, equals('Test U')); // 截取前6位
      expect(user.password, isEmpty); // 苹果登录不需要密码
      expect(user.share, isEmpty);
      expect(user.isVip, isFalse);
      expect(user.like, isEmpty);
    });

    test('should handle long display name', () {
      // Arrange
      const userIdentifier = 'test_user_456';
      const displayName = 'Very Long Display Name';

      // Act
      final user = AppleSignInService.createAppleUser(
        userIdentifier: userIdentifier,
        displayName: displayName,
      );

      // Assert
      expect(user.username.length, lessThanOrEqualTo(6));
      expect(user.username, equals('Very L'));
    });

    test('should handle null display name', () {
      // Arrange
      const userIdentifier = 'test_user_789';

      // Act
      final user = AppleSignInService.createAppleUser(
        userIdentifier: userIdentifier,
      );

      // Assert
      expect(user.username, equals('Apple'));
    });

    test('should create AppleSignInResult with success', () {
      // Arrange & Act
      final result = AppleSignInResult(
        success: true,
        userIdentifier: 'test_123',
        email: 'test@example.com',
        displayName: 'Test User',
      );

      // Assert
      expect(result.success, isTrue);
      expect(result.userIdentifier, equals('test_123'));
      expect(result.email, equals('test@example.com'));
      expect(result.displayName, equals('Test User'));
      expect(result.error, isNull);
      expect(result.isCanceled, isFalse);
    });

    test('should create AppleSignInResult with error', () {
      // Arrange & Act
      final result = AppleSignInResult(
        success: false,
        error: 'Sign in failed',
      );

      // Assert
      expect(result.success, isFalse);
      expect(result.error, equals('Sign in failed'));
      expect(result.userIdentifier, isNull);
    });

    test('should create AppleSignInResult with cancellation', () {
      // Arrange & Act
      final result = AppleSignInResult(
        success: false,
        error: 'User canceled',
        isCanceled: true,
      );

      // Assert
      expect(result.success, isFalse);
      expect(result.isCanceled, isTrue);
      expect(result.error, equals('User canceled'));
    });
  });
}
